$(function () {
    let id = new URLSearchParams(location.search).get('id');
    function getdetail() {
        $.ajax({
            type: "get",
            url: "http://localhost:8888/api/articles/" + id + "/comments",
            success: function (res) {
                console.log(res);
                let paddZero = n => n < 10 ? "0" + n : n;
                template.defaults.imports.filterTime = function (msg) {
                    // msg：形参，就是要处理的数据
                    // 处理完数据之后把结果return出去
                    let d = new Date(msg);
                    let y = d.getFullYear();
                    let m = paddZero(d.getMonth() + 1);
                    let day = paddZero(d.getDate());
                    let h = paddZero(d.getHours());
                    let mm = paddZero(d.getMinutes());
                    let s = paddZero(d.getSeconds());
                    return `${y}-${m}-${day} ${h}:${mm}:${s}`;;
                }
                let htmlStr = template("detaillist", res)
                $(".getdetail").html(htmlStr)
            }
        })
    }
    getdetail();
    // 发表评论
    $('#comment-form').submit(function (e) {
        e.preventDefault()
        let data = $(this).serialize()
        $.ajax({
            type: "post",
            url: "http://localhost:8888/api/articles/" + id + "/comments",
            data,
            success: function (res) {
                if (res.status === 0) {
                    // 发表评论成功
                    layer.msg(res.message);
                    $('#comment-form').get(0).reset();
                    getdetail();
                }
            }
        })
    })
})